package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.ShopProduct;
import com.addplus.townmall.server.api.model.town.StoreMainModuleShop;
import com.addplus.townmall.server.api.model.town.expand.ShopProductExpand;
import com.addplus.townmall.server.api.model.town.expand.StoreMainModuleShopExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商城商家首页模块商品表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface StoreMainModuleShopMapper extends BaseAddMapper<StoreMainModuleShop> {

    @Select("SELECT p.id,p.shop_name,p.shop_seal_img,p.shop_describe,p.sale_price,p.line_price," +
            "p.sale_count+p.sale_virtual_count as sale_count " +
            "FROM store_main_module_shop m " +
            "LEFT JOIN shop_product p ON m.shop_id = p.id " +
            "AND m.store_id = p.store_id AND p.is_deleted = 0 AND p.audit_status=0 AND sale_status !=2 " +
            "WHERE m.store_id = #{store_id} and m.is_deleted = 0 ORDER BY m.sort")
    List<ShopProduct> getStoreMainShopProductByPage(Page page, @Param("store_id") Integer storeId);
    @Select("SELECT ms.id,ms.store_id,s.store_name,ms.shop_id,p.shop_name,ms.store_main_module_id,m.`name` as store_main_module_name,ms.sort,ms.gmt_modified FROM store_main_module_shop ms LEFT JOIN store s on s.id=ms.store_id LEFT JOIN shop_product p on ms.shop_id=p.id LEFT JOIN store_main_module m on m.id=ms.store_main_module_id where ms.is_deleted=0 AND ms.store_id=#{store_id} ORDER BY ms.gmt_modified DESC")
    List<StoreMainModuleShopExpand> getAllStoreMainModuleShop(IPage page,@Param("store_id")Integer store_id);

    @Select("SELECT ms.store_id,s.store_name,ms.shop_id,p.shop_name,ms.store_main_module_id,m.`name` as store_main_module_name,ms.sort,ms.gmt_modified FROM store_main_module_shop ms LEFT JOIN store s on s.id=ms.store_id LEFT JOIN shop_product p on ms.shop_id=p.id LEFT JOIN store_main_module m on m.id=ms.store_main_module_id where ms.is_deleted=0 AND ms.id=#{id} ORDER BY ms.gmt_modified DESC")
    StoreMainModuleShopExpand getStoreMainModuleShopById(@Param("id")Integer id);

    @Select("select IFNULL(max(sort)+1,1) as sort from store_main_module_shop where is_deleted=0")
    Integer getMaxSort();

    @Select("SELECT p.id,p.shop_name,p.shop_seal_img,p.shop_seal_img_pc,p.shop_describe,p.sale_price,p.line_price," +
            "p.sale_count+p.sale_virtual_count as sale_count,sp.shop_comment_count as shop_comment_count,sp.shop_goods as shop_goods, " +
            "s.store_name as store_name,sku.id as shop_sku_id " +
            "FROM store_main_module_shop m " +
            "LEFT JOIN shop_product p ON m.shop_id = p.id " +
            "AND m.store_id = p.store_id AND p.is_deleted = 0 AND p.audit_status=0 AND sale_status !=2 " +
            "LEFT JOIN shop_product_describe sp on sp.shop_product_id = p.id and sp.is_deleted = 0 " +
            "LEFT JOIN store s on s.id = m.store_id and s.is_delete = 0 " +
            "LEFT JOIN shop_specification_sku sku on sku.shop_product_id = p.id and sku.is_deleted = 0 and sku.is_default = 0 " +
            "WHERE m.store_id = #{store_id} and m.is_deleted = 0 ORDER BY m.sort")
    List<ShopProductExpand> getStoreMainShopProductByPagePc(Page page, @Param("store_id") Integer storeId);

}
